<template>
  <div class="form-add">
    <el-form :model="formObj" class="demo-form" ref="formTab" label-width="100px">
      <el-form-item label="电影标题">
         <el-input v-model="formObj.title" placeholder="电影标题" ></el-input>
      </el-form-item>
      <el-form-item label="主演">
        <el-input v-model="formObj.actors" placeholder="主演" ></el-input>
      </el-form-item>

      <!-- :on-success="handleSuccess"
            :on-change="handleChnage"
            :multiple="false"
            :on-remove="handleRemove" -->

      <el-form-item label="主演图片">
        <!-- :auto-upload="false"  阻止图片立即上传  action :必填， 不管是否立即上传图片，action属性不能少-->
        <!-- :on-change="function test(file, fileList){ handlerChange(file, fileList,'tet')  }" -->
        <el-upload
          action=""
          list-type="picture-card"
          :auto-upload="false"
          :on-change="(file, fileList)=>{ handlerChange(file, fileList,'imgSrc') }"
          >
        <i class="el-icon-plus"></i>
      </el-upload>

       
        
      </el-form-item>
      <el-form-item label="导演">
        <el-input v-model="formObj.director" placeholder="导演" ></el-input>
      </el-form-item>
      <el-form-item label="评分">
        <el-input v-model="formObj.score" placeholder="评分" ></el-input>
      </el-form-item>
      <el-form-item label="放映时间">
        <el-date-picker
          v-model="formObj.release"
          type="date"
          placeholder="放映时间">
        </el-date-picker>
   
      </el-form-item>

      
      <el-form-item label="影片类型">
         <el-select v-model="formObj.movieType" placeholder="影片类型" >
          <el-option  v-for="item in typeList" :key="item._id" :label="item.name" :value="item.id"></el-option>
         </el-select>  
      </el-form-item>

      <el-form-item label="语言">
         <el-select v-model="formObj.language" placeholder="语言" >
          <el-option  v-for="item in languageList" :key="item._id" :label="item.language" :value="item.language"></el-option>
         </el-select>  
      </el-form-item>
   
      <el-form-item label="所属区域">
         <el-select v-model="formObj.region" placeholder="所属区域" >
          <el-option  v-for="item in regionList" :key="item._id" :label="item.cname" :value="item.cname"></el-option>
         </el-select>  
      </el-form-item>
      <el-form-item label="时长">
        <el-input v-model="formObj.duration" placeholder="时长" ></el-input>
      </el-form-item>

      <el-form-item label="描述">
        <el-input  type="textarea" v-model="formObj.desc" placeholder="描述" ></el-input>
      </el-form-item>

      <el-form-item label="电影海报">
        <el-upload
          action=""
          list-type="picture-card"
          :auto-upload="false"
          :on-change="(file, fileList)=>{ handlerChange(file, fileList,'posterSrc') }"
          >
        <i class="el-icon-plus"></i>
      </el-upload>
      </el-form-item>
      <el-form-item label="宣传片">
        <el-upload
          action=""
          list-type="picture-card"
          :auto-upload="false"
          :on-change="(file, fileList)=>{ handlerChange(file, fileList,'trailerLink') }"
          >
        <i class="el-icon-plus"></i>
      </el-upload>
      </el-form-item>

      <el-form-item>
            <el-button type="primary" @click="submitForm('formTab')">提交</el-button>
            <el-button @click="resetForm('formTab')">重置</el-button>
        </el-form-item>

    </el-form>
  </div>
</template>

<script>
import axiosEl from '@/utils/axios'
 
export default {
  data(){
    return {
      formObj:{
        imgSrc:null,
        posterSrc:null,
        trailerLink:null
      },
      languageList:[],
      regionList:[],
      typeList:[],
      typeList:[],

      headerObj:{
           Authorization:'Bearer '
       },
    }
  },
  methods:{

    handlerChange(file, fileList,kind){
      // kind - > imgSrc  posterSrc  trailerLink
      if(fileList.length>0) {
        this.formObj[kind] = fileList[0].raw;

      } 
    },
 
   async addMovie(){

     let resData = await axiosEl({
        method:'post',
        url:'/movie/add',
        headers:{
              'Content-Type':'multipart/form-data'
        },
        data:this.formObj
      })

      if(resData.code==1) {
        console.log(resData);
        
      }
    },
 
    getLanguage(){
      axiosEl.get('/language/all').then((resData)=>{
          if(resData.code==1) {
              this.languageList = resData.data;
          }
       })
    },
    getRegionList(){
      axiosEl.get('/region/all').then((resData)=>{
          if(resData.code==1) {
              this.regionList = resData.data;
          }
       })
    },
    getMovieType(){
      axiosEl.get('/type/all').then((resData)=>{
          if(resData.code==1) {
              this.typeList = resData.data;
          }
       })
    },

    submitForm(){
        this.addMovie();
    }
  },
  mounted(){
    let token = localStorage.getItem('token');
    this.headerObj = {
          Authorization:'Bearer '+token
    }

    this.getLanguage();
    this.getRegionList();
    this.getMovieType();
  }
}
</script>

<style>
.form-add {
  width: 600px;
}
</style>